;(function($){
	$.fn.dishZf = function(opt){
		var el = this;
		var opts = {
			bmh : "",
			njson : [],
			zfArr : {},
			ds : [],
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opts,opt);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		var t = this;
		this.initHtml();
		this.getBtnDs(function(){
			t.initBigClass();
			t.bindEvent();
		})
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//取消
			el.find("#btnCancel").click(function(){
				el.empty();
				el.modal("hide");
				param.closeBack();
			})
			
			//确定
			el.find("#btnSure").click(function(){
				debugger
				var text = el.find("#remark").val();
				$(".each_undish[id="+param.ds.xh+"]").find(".eachZf").html(text);
				param.ds.zf = text;
				$(".each_undish[id="+param.ds.xh+"]").find(".jsone").html(JSON.stringify(param.ds));
				
				$(param.njson).each(function(key,value){
					if(value.xh==param.ds.xh)
						param.njson[key]=$.extend({},param.njson[key],param.ds);
				})
				$(".each_undish[id="+param.ds.xh+"]").find(".eachZf").html(text).removeClass("hide");   
				
				t.wait();
				for(var k in param.zfArr){
//					t.addZf(param.zfArr[k]);
					param.addZfDish(param.zfArr[k])
				};
				t.goon();
				el.empty();
				param.dataBack();
				el.modal("hide");
			})
			
			//手写
    		el.find("#remark_btn").unbind("click").bind("click",function(){
    			window.location.href = "handwriter://"; 
    			el.find("#remark").focus();
    		});
    		
    		//退格
    		el.find("#singleDel").unbind("click").bind("click",function(){
    			
    			var searchTableNo = el.find("#remark").val();
				var value = $(this).data("content");
				if(searchTableNo){
					searchTableNo = searchTableNo.substr(0,searchTableNo.length-1);
					el.find("#remark").val(searchTableNo);
				}
    		})
    		
    		//退格
    		el.find("#clearAll").unbind("click").bind("click",function(){
    			el.find("#remark").val("");
    		})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static")
    		var html =	'<div class="modal-dialog remark_order remark_dish" style="width:800px;">'+
				    		'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content" style="width:800px;">'+     
					  			'<div class="modal-header no-padding">'+
									'<div class="table-header widget-header-large " style="text-align:center; font-size:20px">'+  
										'<span>'+$.i18n.propJsp("作法")+'</span>'+       
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+ 
									'<div id="selDoWork" class="col-sm-12" style="border-bottom:1px solid #428bca;display:flex;">'+
										'<div class="input-group modal-remark" id="score_card">'+
											'<span class="input-group-addon">'+
												''+$.i18n.propJsp("作法:")+
											'</span>'+
											'<textarea class="form-control search-query" placeholder="" name="disrate" id="remark"></textarea>'+
											'<span class="input-group-btn" style="display:flex;flex-direction:column;">'+
												'<button type="button" class="btn btn-primary btn-sm" id="remark_btn">'+
													'<span class="ace-icon fa fa-pencil-square-o icon-on-right bigger-110"></span>'+
												'</button>'+
												'<button type="button" class="btn btn-primary btn-sm" id="singleDel" style="margin-top:5px;">'+
													''+$.i18n.propJsp("退格")+
												'</button>'+
												'<button type="button" class="btn btn-primary btn-sm" id="clearAll" style="margin-top:5px;">'+
													''+$.i18n.propJsp("清空")+
												'</button>'+
											'</span>'+
										'</div>'+
									'</div>'+
									'<div class="col-sm-12 no-padding" style="display:flex;">'+
										'<div id="bigTypeInfo" style="width:105px;display:flex;flex-direction:column;border-right:1px solid #cccccc;padding:3px;">'+
											
										'</div>'+
										'<div id="checkoutInfo" style="flex:1;padding:3px;height:326px;overflow-y: auto;">'+
										
										'</div>'+
									'</div>'+
								'</div>'+	
								'<div class="modal-footer">'+
									'<button id="btnSure" class="btn btn-info btn-lg btn-round">'+  
										'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
									'</button>'+
									'<button id="btnCancel" class="btn btn-default btn-lg btn-round" data-dismiss="modal">'+  
										'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+'</button>'+ 
									'</button>'+
								'</div>'+		
			  				'</div>'+
			  			'</div>';		
			
			el.html(html);
		},
		initBigClass : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.find("#remark").val(param.ds.zf);
			
//			t.getBigClassDs(function(data){
				var data = param.Practicehead
				//大类HTML
				var bigTypeHtml = ''
				$(data).each(function(k,v){
					var primary = "";
					if(k==0){
						primary = "btn-primary";
					}
					bigTypeHtml += 	'<div class="btn-group">'+
										'<button class="btn btn-sm bigtype '+primary+'" data-type="'+v.type+'" style="margin:1px;width:97px;height:38px;">'+
											v.type+
										'</button>'+
									'</div>';
				})
				el.find("#bigTypeInfo").html(bigTypeHtml);
				
				//绑定大类事件
				el.find(".bigtype").unbind("click").click(function(){
					el.find(".bigtype.btn-primary").removeClass("btn-primary");
					$(this).addClass("btn-primary");
					param.type = $(this).data("type");
					t.initSmallClass();
				});
				
				el.find(".bigtype.btn-primary").trigger("click");
//			})
			
		},
		initSmallClass : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var smallHtml = "";
			
    		var filter = "select * from json where (type=='"+param.type+"')";
    		var data = jsonsql.query(filter,param.Practicedetail);
    		
//			t.getSmallClassDs(function(data){
				$(data).each(function(k,v){
					var text = el.find("#remark").val()
					
					var primary = "";
					if(text.indexOf(v.mc)>-1){
						primary = "btn-primary";
					}
					smallHtml +='<div class="btn-group">'+
									'<button data-mc="'+v.mc+'" class="btn btn-sm detailInfo '+primary+'" style="margin:1px;width:104px;height:38px;padding:4px 2px;font-size:12px;">'+
										'<span class="hide">'+JSON.stringify(v)+'</span>'+
										v.mc+
									'</button>'+
								'</div>';
				})
				el.find("#checkoutInfo").html(smallHtml);
				
				el.find(".detailInfo").unbind("click").click(function(){
					var $this = $(this);
					var json = $.parseJSON($this.find("span").eq(0).text());
					t.operZf($this,json);
				});
				
//			})
		},
		getBigClassDs : function(back){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			t.wait();
			var arg = {"arg":{"arg":"备注类别"}};
			jQuery.ajax({  
	    		type : 'POST',
	    		url :  $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
	    		data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'POSMenuMessage',"{}","list"),
	    		dataType : 'json',  
	    		success : function(data){
	    			t.goon();
	    			back(data);
	    		},  
	    		error : function(data){
	    			t.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		getSmallClassDs : function(back){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			t.wait();
			var wherestring = {};
			wherestring = {wherestring:["@@WS","type = '"+param.type+"' and bmh='"+param.bmh+"'"]}
			var where = {};
			where.where = {1:1};
			var fileds = {};
			fileds.fileds = {"fileds":["bh","croup_name","mc","flag","pym","type","item","price"]};
			                           
			var table={};
			table.table={"table":"ct_message"};				
			c= jQuery.extend({}, where,fileds,table,wherestring);
			jQuery.ajax({  
				type : 'POST',
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
				data : $.HSAPI_ASKJSONINFO(JSON.stringify(c),'ExecRetrieveJson','{}'),
				dataType : 'json',  
				success : function(data){
					t.goon();
//					alert(JSON.stringify(data));
					console.log("JSON:::"+JSON.stringify(data));
		            if(data.RETURN == 1){
		            	back(data.DATA)
		            }else if(data.RETURN == 0){
		            	back([])
		            }else{
		            	back([])
		            	jf_alert(data.MSG);
					}
				},  
				error : function(data){ 
					t.goon();
					jf_alert(JSON.stringify(data));
				}  
			});
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
		operZf : function($this,json){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var text = el.find("#remark").val()
			var arr = (text)?text.split(","):[];
			var json = $.parseJSON($this.find("span").eq(0).text());
			var mc = $this.data("mc");
			
			if($this.hasClass("btn-primary")){
				$this.removeClass("btn-primary")
				$(arr).each(function(k,v){
					if(v.indexOf(mc)>-1){
						arr.splice(k, 1);
					}
					if(param.zfArr[mc]){
						delete param.zfArr[mc]
					}
				})
				el.find("#remark").val(arr.join(","));
			}else{
				$this.addClass("btn-primary");
				var sl = "1.00";
				if(mc.indexOf("$")>-1&&json.price){
					t.editNum(json);
				}else{
					arr.push(mc);
					el.find("#remark").val(arr.join(","));
				}
			}
		},
		editNum : function(json){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var status = true;
			var title = json.mc + "&nbsp;-&nbsp;数量";
			
			var text = el.find("#remark").val()
			var arr = (text)?text.split(","):[];
			var mc = json.mc;
			
			if(!$.isFunction($().digitalCalculator)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
	            $.ajaxSetup({ async: true });
			}
			$("#mykeyboard").digitalCalculator({
				number:1,//数量
				type : "pNum",//pNum:人数;discount:小数折扣;percent:百分比;price:两位小数;
				status:status?status:true,
				title:title?title:"",
				//初始化数据返回回调
				dataBack:function(number){
					json.dj = json.price;
					json.bh = (json.item||"00000000");
					json.sl = number//+".00";
					json.je = parseFloat(json.price) * parseFloat(json.sl)
					var mc_c = json.mc+"("+json.dj+"/"+json.dw+"x"+number+")"
					json.mc_c = mc_c;
					param.zfArr[mc] = json;
					arr.push(mc_c)
					el.find("#remark").val(arr.join(","));
				},
				returnBack : function(){
					json.dj = json.price;
					json.bh = (json.item||"00000000");
					json.sl = "1.00";
					json.je = parseFloat(json.price) * parseFloat(json.sl)
					var mc_c = json.mc+"("+json.dj+"/"+json.dw+"x"+1+")"
					json.mc_c = mc_c;
					param.zfArr[mc] = json;
					arr.push(mc_c)
					el.find("#remark").val(arr.join(","));
				}
			});
		},
		addZf : function(json){
			
			var el = this.el;
			var t = this;
			var param = this.param;
			
			var ds = param.ds;
			var njson = param.njson;
			
			var bharr=[];//收集bh数组
			$(param.njson).each(function(key,value){
				bharr.push(value.bh);
			})
			var index=bharr.indexOf(ds.bh);
			
			var id = $(".each_undish[data-bh="+ds.bh+"]").attr("id");
			var id1 = id + 1;
			json.sl = "1.00";
			json.dj = toFloot(json.price);
			var html=	'<div class="each_undish" id="'+id1+'" data-bh="'+json.bh+'">'+
							'<div class="jsone hide">'+ JSON.stringify(json) +'</div>'+
							'<div class="btn btn-app btn-light no-radius dish_content" data-id="tableView">'+
								'<div class="each_top flex">'+
									'<div class="eachName flex">'+
										'<div class="dish_id">'+ id +'</div>&nbsp;<div data-name="'+json.mc+'" class="dish_name">'+ json.mc +'</div>'+
									'</div>'+
									'<div class="eachRight flex">'+
										'<div class="eachPirce">'+ toFloot(json.price)+'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="sub_dishes"></div>'+
						'</div>';
			$(".each_undish[data-bh="+ds.bh+"]").after(html);
			param.njson.splice(id, 0, json);
			$(".each_undish[data-bh]").each(function(k,v){
				$(this).attr("id",(k+1));
				$(this).attr("tabindex",(10000+k));
				$(this).find(".dish_id").html(k+1);
			})
		},
		getBtnDs : function(back){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			t.wait();
			jQuery.ajax({  
				type : 'POST',
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
				data : $.HSAPI_ASKJSONINFO(JSON.stringify(c),'POSCppractice','{}'),
				dataType : 'json',  
				success : function(data){
					t.goon();
		            if(data.RETURN == 1){
		            	param.Practicehead = data.Practicehead;
		            	param.Practicedetail = data.Practicedetail;
		            }else{
		            	param.Practicehead = [];
		            	param.Practicedetail = [];
					}
		            back();
				},  
				error : function(data){ 
					t.goon();
					jf_alert(JSON.stringify(data));
				}  
			});
		}
	}
	
})(jQuery);